<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Boolean隐式转换原理</title>
</head>
<body>
    <script>
        // 原理
        const boolean = new Boolean(false);
        console.log(typeof boolean);
        console.log(boolean.valueOf());
        if(boolean.valueOf()){
            console.log("值为真");
        }

        // 简单的字面量形式
        const bool = true;
        if(bool){
            console.log("值为真");
        }


        // 比较和转换是两件事

        // 比较的时候都会转化成数值类型
        const str = "";
        console.log(Number(str));
        if(str == false) console.log("字符串为空时，为假");

        const str1 = '3';
        console.log(Number(str1));
        if(str1 == true) console.log("字符串为1时，是真的");

        // 这边是转化成Boolean类型的，字符串不为空时，都为真
        if(Boolean(str1)){
            console.log("字符串不为空时，都为真");
        }



        const arr = [];
        console.log(Number(arr));//0
        console.log(arr == false);//true

        const arr1 = [1];
        console.log(Number(arr1));//1
        console.log(arr1 == true);//true

        const arr2 = [1,2,3];
        console.log(Number(arr2));//NaN
        console.log(arr2 == true);//false



        // 空的引用类型，转换成boolean类型，值为真
        console.log(Boolean([]));//true
        console.log(Boolean({}));//true

    </script>
</body>
</html>